package com.lovo.rb.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
     //创建队列
    @Bean
    public Queue oneQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadDirectExchange");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","dead.ticket");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.durable("oneQueue").withArguments(args).build();
    }
    @Bean
    public Queue deadQueue(){
        return new Queue("deadQueue");
    }
    @Bean
    public Queue twoQueue(){
        return new Queue("twoQueue");
    }
    @Bean
    public Queue threeQueue(){
        return new Queue("threeQueue");
    }
    @Bean
    public Queue fourQueue(){
        return new Queue("fourQueue");
    }

    //创建换机
    @Bean
    public DirectExchange directExchangeOne(){
        return new DirectExchange("directExchangeOne");
    }
    //主题交换机
    @Bean
    public TopicExchange topicExchangeTwo(){
        return  new TopicExchange("topicExchangeTwo");
    }
    //扇型交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }
   //死信交换机
    @Bean
    public DirectExchange deadDirectExchange(){
        return new DirectExchange("deadDirectExchange");
    }
    //绑定队列与交换机
    @Bean
   public Binding oneQueueToDirectExchangeOne(Queue oneQueue,DirectExchange directExchangeOne){
    return BindingBuilder.bind(oneQueue).to(directExchangeOne).with("movie.ticket");
   }
   @Bean
   public Binding twoQueueToTopicExchangeTwo(Queue twoQueue,TopicExchange topicExchangeTwo){
     return BindingBuilder.bind(twoQueue).to(topicExchangeTwo).with("order.#");
   }

   @Bean
   public Binding threeQueueToFanoutExchange(Queue threeQueue,FanoutExchange fanoutExchange){
    return BindingBuilder.bind(threeQueue).to(fanoutExchange);
   }
    @Bean
    public Binding fourQueueToFanoutExchange(Queue fourQueue,FanoutExchange fanoutExchange){
     return BindingBuilder.bind(fourQueue).to(fanoutExchange);
    }
    @Bean
    public Binding deadQueueToDeadDirectExchange(Queue deadQueue,DirectExchange deadDirectExchange){

        return BindingBuilder.bind(deadQueue).to(deadDirectExchange).with("dead.ticket");
    }
}
